package com.future.direction.net.rto_subscriber;

import android.content.Context;
import android.content.Intent;

import com.blankj.utilcode.util.ActivityUtils;
import com.future.direction.common.Constant;
import com.future.direction.common.util.JsonUtils;
import com.future.direction.common.util.LogUtil;
import com.future.direction.common.util.SharePreferencesUtils;
import com.future.direction.common.util.UIUtil;
import com.future.direction.common.util.UserCacheUtil;
import com.future.direction.data.bean.BaseBean;
import com.future.direction.net.rto_exception.ApiException;
import com.future.direction.net.rto_exception.BaseException;
import com.future.direction.ui.activity.LoginActivity;
import com.future.direction.ui.widget.BaseView;

import java.io.IOException;

import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;
import retrofit2.HttpException;

/**
 * Created by ZHP on 2017/6/25.
 */

public abstract class ProgressSubcriber<T> extends ErrorHandlerSubscriber<T> {
    private BaseView mView;


    public ProgressSubcriber(Context context, BaseView view) {
        super(context);
        this.mView = view;

    }


    public boolean isShowProgress() {
        return true;
    }


    @Override
    public void onSubscribe(Disposable d) {
        super.onSubscribe(d);
        if (isShowProgress() && null != mView) {
            mView.showLoading();
        }
    }

    @Override
    public void onComplete() {
        if (null != mView) {
            mView.dismissLoading();
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        if (e instanceof ApiException) {
            ApiException a = (ApiException) e;
            LogUtil.d("api", "------" + a.getDisplayMessage() + "------" + a.getCode());
            //TODO 登录失效、错误
            if (a.getCode().equals(BaseException.ERROR_TOKEN) ||
                    a.getCode().equals(BaseException.NOT_LOGIN) ||
                    a.getCode().equals(BaseException.GO_LOGIN)) {
                SharePreferencesUtils.clearUserParts();
                ActivityUtils.finishAllActivities();
                toLogin();
            }
            if (null != mView) {
                mView.showError(a.getDisplayMessage());
            }
        } else if (e instanceof HttpException) {
            ResponseBody body = ((HttpException) e).response().errorBody();
            try {
                if (body.string().contains("<head>")) {

                } else {
                    BaseBean baseBean = JsonUtils.fromJson(body.string(), BaseBean.class);
                    mView.showError(null != baseBean ? baseBean.getMsg() : "");
                }
            } catch (IOException IOe) {
                IOe.printStackTrace();
            }
        } else {
            ApiException exception = new ApiException("500", "");
            if (null != mView) {
                mView.showError(exception.getDisplayMessage());
            }
        }
        if (isShowProgress()) {
            if (null != mView) {
                mView.dismissLoading();
            }
        }

    }

    private void toLogin() {
        Intent intent = new Intent(UIUtil.getContext(), LoginActivity.class);
        intent.putExtra(Constant.main, Constant.TAB_HOME);
        UIUtil.startActivity(intent);
    }


}
